package com.eva.news.cms.databuilder.builders.cms;

import com.eva.news.cms.biz.category.CmsCategoryBiz;
import com.eva.news.cms.biz.category.vo.CmsCategoryNodeVO;
import com.eva.news.cms.databuilder.AbstractTemplateDataBuilder;
import com.eva.news.cms.databuilder.bo.TemplateDataParameters;
import com.eva.news.framework.cache.autowired.Cache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 构建子栏目树数据
 */
@Slf4j
@Component
public class SubCategoryTreeBuilder extends AbstractTemplateDataBuilder<List<CmsCategoryNodeVO>> {

    private final static String PARAMETER_PARENT_CATEGORY_UID = "parentCategoryUid";

    @Resource
    private CmsCategoryBiz cmsCategoryBiz;

    // 永久缓存，后台操作栏目时会清理此缓存
    @Cache(value = "category:{#parameters.getString('" + PARAMETER_PARENT_CATEGORY_UID + "', null)}:children")
    public List<CmsCategoryNodeVO> build(TemplateDataParameters parameters) {
        // v1.3.0调整，统一查询栏目树方法
        return cmsCategoryBiz.findSubTree(
                parameters.getString(PARAMETER_PARENT_CATEGORY_UID, null),
                Boolean.FALSE,
                parameters);
    }
}
